package com.edt.ichannel.api.controller;

import cc.edt.iceutils2.string.IceStringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.edt.ichannel.api.common.controller.BaseController;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.OrderChannel;
import com.edt.ichannel.api.service.GawoTwoService;
import com.edt.ichannel.api.service.OrderChannelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 工单二期接口类
 * @author 肖瑶
 */
@Controller
@RequestMapping("/api/order")
public class GawoTwoController extends BaseController {

    @Resource
    private GawoTwoService gawoTwoService;

    @Resource
    private OrderChannelService orderChannelService;
    /**
     * 意向单(透传/抢派)接口
     * @param request request
     * @throws IOException
     */
    @RequestMapping(value = "collectorder_pass",method = RequestMethod.POST)
    public void collectorder(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        BufferedReader reader = request.getReader();
        StringBuffer body =  new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null){
            body.append(line);
        }
        if(body == null){
            actionResult.setSuccess(false);
            actionResult.setMessage("json解析错误");
            WriterToPageByJson(actionResult);
        }
        OrderChannel orderChannel = JSONObject.parseObject(body.toString(),OrderChannel.class);
        actionResult = gawoTwoService.collectorder(orderChannel);
        WriterToPageByJson(actionResult);
    }

    /**
     * 正式单(透传/抢派)接口
     * @param request req
     * @throws IOException
     */
    @RequestMapping(value = "collectorder",method = RequestMethod.POST)
    public void formalOrder(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        BufferedReader reader = request.getReader();
        StringBuffer body =  new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null){
            body.append(line);
        }
        if(body == null){
            actionResult.setSuccess(false);
            actionResult.setMessage("json解析错误");
            WriterToPageByJson(actionResult);
        }
        OrderChannel orderChannel = JSON.parseObject(body.toString(),OrderChannel.class);
        actionResult = gawoTwoService.formalOrder(orderChannel);
        WriterToPageByJson(actionResult);
    }


    /**
     * 退单接口
     * @param request req
     * @throws IOException
     */
    @RequestMapping(value = "quitorder",method = RequestMethod.POST)
    public void chargeback(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        BufferedReader reader = request.getReader();
        StringBuffer body =  new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null){
            body.append(line);
        }
        if(body == null){
            actionResult.setSuccess(false);
            actionResult.setMessage("json解析错误");
            WriterToPageByJson(actionResult);
        }
        String ordercode = JSONObject.parseObject(body.toString()).getString("ordercode");
        if(IceStringUtils.isBlank(ordercode)){
            actionResult.setSuccess(false);
            actionResult.setMessage("订单流水为空!");
            WriterToPageByJson(actionResult);
        }
        String remark = JSONObject.parseObject(body.toString()).getString("remark");
        actionResult = orderChannelService.orderBack(ordercode,"4",remark);
        WriterToPageByJson(actionResult);
    }

    @RequestMapping(value = "orderresult",method = RequestMethod.POST)
    public void orderresult(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        BufferedReader reader = request.getReader();
        StringBuilder body =  new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null){
            body.append(line);
        }
        String ordercode = JSONObject.parseObject(body.toString()).getString("ordercode");
        if(IceStringUtils.isBlank(ordercode)){
            actionResult.setSuccess(false);
            actionResult.setMessage("订单流水为空!");
            WriterToPageByJson(actionResult);
        }
        String orderstate = JSONObject.parseObject(body.toString()).getString("orderstate");
        if(IceStringUtils.isBlank(orderstate)){
            actionResult.setSuccess(false);
            actionResult.setMessage("订单状态为空!");
            WriterToPageByJson(actionResult);
        }
        String remark = JSONObject.parseObject(body.toString()).getString("remark");
        actionResult = orderChannelService.orderBack(ordercode,orderstate,remark);
        WriterToPageByJson(actionResult);
    }

}
